﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace WebUI.Teacher
{
    public partial class SendReservation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnCancel.OnClientClick = ActiveWindow.GetConfirmHideReference();
            BindData();
        }

        private void BindData()
        {
            string LabID = Request.QueryString["labid"];
            DateTime date = Convert.ToDateTime(Request.QueryString["date"]);
            string lessonID = Request.QueryString["lessonid"];

            Model.Lab m = BLL.LabManage.SelectLabByID(int.Parse(LabID));
            lb_labname.Text = m.LabName;

            lb_user.Text = User.Identity.Name;

            lb_date.Text = string.Format("{0:D}", date);

            switch (lessonID)
            {
                case "56":
                    lb_lesson.Text = "1,2节课";
                    break;
                case "57":
                    lb_lesson.Text = "3,4节课";
                    break;
                case "58":
                    lb_lesson.Text = "5,6节课";
                    break;
                case "59":
                    lb_lesson.Text = "7,8节课";
                    break;
            }
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            Model.Users u = BLL.UsersManage.SelectByUsername(User.Identity.Name);
            int LabID = int.Parse(Request.QueryString["labid"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["date"]);
            int lessonID = int.Parse(Request.QueryString["lessonid"]);
            Model.OpenReservation m = new Model.OpenReservation();
            m.OpenLabID = LabID;
            m.userid = u.id;
            m.used = tb_used.Text;
            m.date = date;
            m.lesson = lessonID;
            m.confirmed = false;
            int ResID = BLL.OpenReservationMange.Add(m);
            Model.OpenLabStatus mStatus;
            bool exist = BLL.OpenLabStatusManage.Exists(LabID, date);
            if (exist)
            {
                mStatus = BLL.OpenLabStatusManage.SelectOneByPrimaryKey(LabID, date);
            }
            else
            {
                mStatus = new Model.OpenLabStatus();
                mStatus.OpenLabID = LabID;
                mStatus.date = date;
            }

            switch (lessonID)
            {
                case 56:
                    mStatus.lesson12 = ResID;
                    break;
                case 57:
                    mStatus.lesson34 = ResID;
                    break;
                case 58:
                    mStatus.lesson56 = ResID;
                    break;
                case 59:
                    mStatus.lesson78 = ResID;
                    break;
            }
            if (exist)
            {
                if (BLL.OpenLabStatusManage.Update(mStatus))
                {
                    PageContext.RegisterStartupScript(Alert.GetShowInTopReference("预约成功，请等待管理员审核！", "操作提示！", MessageBoxIcon.Information) + ActiveWindow.GetHideRefreshReference());
                }
                else
                {
                    Alert.Show("遇到未知错误，预约失败！请联系管理员");
                }
            }
            else
            {
                if (BLL.OpenLabStatusManage.Add(mStatus))
                {
                    PageContext.RegisterStartupScript(Alert.GetShowInTopReference("预约成功，请等待管理员审核！", "操作提示！", MessageBoxIcon.Information) + ActiveWindow.GetHideRefreshReference());
                }
                else
                {
                    Alert.Show("遇到未知错误，预约失败！请联系管理员");
                }
            }
        }
    }
}